

clear
set more 1
set type double

*****************************************************************
*prepares currency variables
*i. dummy indicator for euro vs other
*ii. share variable for euro and dollar
*iii. vehicle currency indicator
*******************************************************************************************
		
**************************************************	
*i. time-invariant exporter currency choice - EURO vs other
* constructed at vat-destination level
**********************************************
*i. dummy indicator 
*********************************************
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop if missing(currency)
	drop *I
	drop if missing(valueX)
	rename country cty_code
    	
    	gen euro="prod" if currency=="EUR"
    	replace euro="other" if currency~="EUR"
  
    	collapse (sum) value=valueX, by(vat cty_code euro)
    	    	
    	duplicates tag vat cty_code, gen(tag)
    	bys vat cty_code (value): gen test=euro[_N]
    	egen totvalue=sum(value), by(vat cty_code)
    	gen same=test==euro
*code to only keep largest value one    	
	drop if tag==1 & same==0

	keep vat cty_code euro 
    	save temp/currency_vat, replace    	

*ii. share indicator
*combines currency indicator with share variable here
**************************************************
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop if missing(currency)
	drop *I
	drop if missing(valueX)
	rename valueX value
	drop if missing(value)
	rename country cty_code
    	drop if missing(currency)
    	gen euro_dum= currency=="EUR"
    	gen USD_dum=currency=="USD"
    	
    	gen value_euro=value*euro_dum
    	gen value_dollar=value*USD_dum
    	
    	collapse (sum) value*, by(vat cty_code)
    	
    	gen share_euro=value_euro/value
    	gen share_usd=value_dollar/value
    	keep vat cty_code share_euro share_usd
    	merge 1:1 vat cty_code using temp/currency_vat, nogen
        label var euro "prod indicates more than 50% share of exports within vat-destination in euro, otherwise other"
        label var share_euro "share of exports in 2017-19 in euro"
        label var share_usd "share of exports in 2017-19 in usd
    save ../data/currency_vat, replace    
    
************************************************************ 
**************************************************	
*iii. furthe split by vehicle 
*time invariant currency choice - EURO vs Local vs Vehicle
**********************************************
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop if missing(currency)
	drop *I
	drop if missing(valueX)
	rename valueX value
	drop if missing(value)
	rename country cty_code

	merge m:1 cty_code using ../rawdata/iso_cty_and_currency, keep(match) nogen
	replace cty_name="User" if cty_code=="QW"
	drop if missing(currency_code)

	merge m:1 cty_code using ../data/usd_peg, keep(match master) nogen
	gen currency_code_peg=currency_code
	replace currency_code_peg="USD" if usd_peg==1

	gen currency_cat="prod" if currency=="EUR"
	replace currency_cat="local" if currency==currency_code & missing(currency_cat)
	replace currency_cat="vehicle" if missing(currency_cat)

	gen vehicle_other=currency_cat=="vehicle" & currency~="USD"

	gen currency_cat_peg="prod" if currency=="EUR"
	replace currency_cat_peg="local" if (currency==currency_code|currency==currency_code_peg) & missing(currency_cat_peg)
	replace currency_cat_peg="vehicle" if missing(currency_cat_peg)

	gen value_prod=value if currency_cat=="prod"
	gen value_local=value if currency_cat=="local"
	gen value_vehicle=value if currency_cat=="vehicle"

	gen value_local_peg=value if currency_cat_peg=="local"
	gen value_vehicle_peg=value if currency_cat_peg=="vehicle"
		
	gen value_vehicle_other=value * (vehicle_other==1)
		
	gcollapse (sum) value* (max) vehicle_other, by(vat cty_code)
	
	gen share_vehicle_other=value_vehicle_other/value

	gen share_prod=value_prod/value
	gen share_local=value_local/value
	gen share_vehicle=value_vehicle/value

	gen share_local_peg=value_local_peg/value
	gen share_vehicle_peg=value_vehicle_peg/value

	keep vat cty_code share* vehicle_other

 label var vehicle_other "indicator of currency = vehicle but non-usd"
 label var share_vehicle_other "share of exports in non-usd vehicle by firm-destination"
 label var share_prod "value share of euro currency"	
 label var share_local "value share of destination currency"
 label var share_local_peg "value share of destination currency or usd for pegged countries" 
 label var share_vehicle "value share of non=prod and non-local ie vehicle includes non-dollar"
 label var share_vehicle_peg "value share of non-prod and non-local and non-usd for pegged"


save ../data/currency_vehicle, replace    	

 
